<?php
include('include/config.inc.php');
$techAlloc = array();
$count = 0;
$techId   = isset($_REQUEST['techId']) ? $_REQUEST['techId'] : 0;
$toDate   = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'];
$fromDate = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'];

if($techId > 0)
{
	$whereCondition = " WHERE technician.technicianId = ".$techId."";
}
else
{
	$whereCondition = NULL;
}

if($toDate > 0)
{
  $formDateCondition = " AND date >='".$fromDate."'";
  $toDateCondition   = " AND date <='".$toDate."'";
}

$selectTechallocation = "SELECT DISTINCT techallocation.complainServiceId AS id,DATE_FORMAT(techallocation.date,'%d-%m-%Y') AS date, techallocation.complainServiceId, techallocation.newinstallationMasterId,
                                techallocation.otherServiceId,techallocation.technicianId,technician.firstName,technician.lastName,
                                complainservice.partyId,party.companyName
                           FROM techallocation
                           JOIN technician ON technician.technicianId = techallocation.technicianId
                           JOIN complainservice ON complainservice.complainServiceId = techallocation.complainServiceId
                           JOIN party ON party.partyId = complainservice.partyId
                                ".$whereCondition."
                                ".$formDateCondition."
                                ".$toDateCondition."
                          UNION 
                         SELECT DISTINCT techallocation.newinstallationMasterId,DATE_FORMAT(techallocation.date,'%d-%m-%Y') AS date, techallocation.complainServiceId, techallocation.newinstallationMasterId,
                                techallocation.otherServiceId,techallocation.technicianId,technician.firstName,technician.lastName,
                                newinstallationmaster.partyId,party.companyName
                           FROM techallocation
                           JOIN technician ON technician.technicianId = techallocation.technicianId
                           JOIN newinstallationmaster ON newinstallationmaster.newinstallationMasterId = techallocation.newinstallationMasterId
                           JOIN party ON party.partyId = newinstallationmaster.partyId
                                ".$whereCondition."
                                ".$formDateCondition."
                                ".$toDateCondition."
                          UNION 
                         SELECT DISTINCT techallocation.otherServiceId,DATE_FORMAT(techallocation.date,'%d-%m-%Y') AS date, techallocation.complainServiceId, techallocation.newinstallationMasterId,
                                techallocation.otherServiceId,techallocation.technicianId,technician.firstName,technician.lastName,
                                otherservice.partyId,party.companyName
                           FROM techallocation
                           JOIN technician ON technician.technicianId = techallocation.technicianId
                           JOIN otherservice ON otherservice.otherServiceId = techallocation.otherServiceId
                           JOIN party ON party.partyId = otherservice.partyId
                                ".$whereCondition."
                                ".$formDateCondition."
                                ".$toDateCondition."
                          ORDER BY date,technicianId";
$techAlloRes = mysql_query($selectTechallocation);
while($techRow = mysql_fetch_array($techAlloRes ))
{ 
  if(isset($techAlloc[$count - 1]['date']) && isset($techAlloc[$count - 1]['technicianId']) && $techAlloc[$count - 1]['date'] == $techRow['date']
     && $techAlloc[$count - 1]['technicianId'] == $techRow['technicianId'])
  {
    if($techAlloc[$count - 1]['companyNameSite2'] == "-")
      $techAlloc[$count - 1]['companyNameSite2'] = $techRow['companyName'];
    elseif($techAlloc[$count - 1]['companyNameSite3'] == "-")
      $techAlloc[$count - 1]['companyNameSite3'] = $techRow['companyName'];
    elseif($techAlloc[$count - 1]['companyNameSite4'] == "-")
      $techAlloc[$count - 1]['companyNameSite4'] = $techRow['companyName'];
    elseif($techAlloc[$count - 1]['companyNameSite5'] == "-")
      $techAlloc[$count - 1]['companyNameSite5'] = $techRow['companyName'];
    else
      $techAlloc[$count - 1]['companyNameSite6'] = $techRow['companyName'];
  }
  else
  {
    $techAlloc[$count]['technicianId'] = $techRow['technicianId'];
    $techAlloc[$count]['date'] = $techRow['date'];
    $techAlloc[$count]['techName'] = $techRow['firstName']." ".$techRow['lastName'];
    $techAlloc[$count]['companyNameSite1'] = $techRow['companyName'];
    $techAlloc[$count]['companyNameSite2'] = "-";
    $techAlloc[$count]['companyNameSite3'] = "-";
    $techAlloc[$count]['companyNameSite4'] = "-";
    $techAlloc[$count]['companyNameSite5'] = "-";
    $techAlloc[$count]['companyNameSite6'] = "-";
    $count++;
  }
}
$smarty->assign("techAlloc",$techAlloc);
$smarty->display("sitetechAj.tpl");
?>